
## ------------------------------------------------------------------- ##
## ----------    UPLOAD DATA & GENERATE MAIN TABLES     -------------- ##
## ------------------------------------------------------------------- ##

# Upload simulated moments for the baseline model
target_mms 	= readdlm("simulated_moments/target_moments.txt", ',', Float64) 	# E[b/y]; E[def]; E[SP]; sd(SP); B/(B+b); Elast
untgt_mms  	= readdlm("simulated_moments/untgt_moments.txt", ',', Float64) 		# sd(C)/sd(Y); sd(TB/Y)/sd(Y); c(C,Y); c(TB/Y,Y); c(SP,Y)
piBE_mms 	= readdlm("simulated_moments/piBE_moments.txt", ',', Float64) 		# E[π]; sd(π); sd(dBE); c(dπ,dlnSP); c(dπ,dBE); c(π,Y)
SPdcmp 		= readdlm("simulated_moments/SPdecomp_moments.txt", ',', Float64) 	# E[RP]; E[RP/SP]; E[RP/SP|Y<1]; sd(RP); c(RP,Y); c(RP/SP,Y);

################# ------------------------------------------------------------------------------------- #################
################# -------------------------------- *** MAIN TABLES *** -------------------------------- #################
################# ------------------------------------------------------------------------------------- #################

### PARAMETERS - CALIBRATION
 open("tables/table_params.txt", "w") do io
 write(io, "\\begin{table}[H] \n")
 write(io, "\\centering \n")
 write(io, "\\begin{tabular}{clcclc} \n")
 write(io, "\\hline \\hline \n")
 write(io, " \\multicolumn{3}{c}{Panel A: Fixed Parameters} & \\multicolumn{3}{c}{Panel B: Calibrated Parameters} \\\\ \n")
 write(io, "\\hline \n")
 write(io, " Param. &  \\hspace{1.1cm} Description  &   Value  & Param. &   \\hspace{1.1cm} Description  &  Value  \\\\\ \n ")
 write(io, "\\hline \n")
 write(io, " \ \$\\gamma\$ 														&  Risk aversion  					& \$$(string(round(ce.γ,2),0))\$   	& 	\ \$\\beta\$ 		&  Discount rate							& \$$(round(ce.β,3))\$  			\\\\  \n")
 write(io, " \ \$z\$ 															&  Coupon payments 					& \$$(round(ce.zb,2))\$   			& 	\ \$\\bar{\\chi}_0\$ 			&  Default cost---level 					& \$$(round(ce.d0,3))\$  			\\\\  \n")
 write(io, " \ \$\\lambda\$ 													&  Debt maturity 					& \$$(round(ce.mb,2))\$   			& 	\ \$\\bar{\\chi}_1\$ 			&  Default cost---curvature  				& \$$(round(ce.d1,4))\$  			\\\\  \n")
 write(io, " \ \$r\$ 															&  Risk-free interest rate  		& \$$(round(ce.r,2))\$  			& 	\ \$\\bar{\\chi}_2\$ 	&  Default cost---differential  			& \$$(round(ce.ε,3))\$  			\\\\  \n")
 write(io, " \ \$T_{jj}\$ 													    &  Persistence j-type  				& \$$(round(ce.ΠT[1,1],3))\$  		& 	\ B 				&  Inflation-indexed debt service		 	& \$$(round(ce.Bval,4))\$  			\\\\  \n")
 write(io, " \ \$\\rho_{{\\ensuremath{\\text{\\miniscule\\normalfont y}}}}\$ 	&  Endowment, autocorrelation  		& \$$(round(ce.ρ,4))\$   			& 	\ \$\\alpha\$ 		&  Probability threshold  					& \$$(round(ce.α,4))\$  			\\\\  \n")
 write(io, " \ \$\\sigma_{{\\ensuremath{\\text{\\miniscule\\normalfont y}}}}\$ 	&  Endowment, shock volatility  	& \$$(round(ce.η,4))\$  			& 	 	 				& 											&  									\\\\  \n")
 write(io, " \ \$\\theta\$ 														&  Reentry probability 				& \$$(round(ce.θ,4))\$  			& 	 	 				& 											&  									\\\\  \n")
 write(io, " \ \$\\sigma\$ 														&  Precision of signal 				& \$$(round(ce.σ_π,4))\$  	    	& 	 	 				& 											&  									\\\\  \n")
 write(io, "\\hline \\hline \n")
 write(io, "\\end{tabular} \n")
 write(io, "\\end{table} \n")
 end

### TARGETED MOMENTS
open("tables/table_targets.txt", "w") do io
write(io, "\\begin{table}[H] \n")
write(io, "\\centering \n")
write(io, "\\begin{tabular}{llcc} \n")
write(io, "\\hline \\hline \n")
write(io, " Target & Description & Data & Model \\\\ \n")
write(io, "\\hline \n")
write(io, "\$\\mathbb{E}[D/Y]\$ 				& Average debt										& \$$(round(Int, target_mms[1,1]))\\%\$ 		& \$$(round(Int, target_mms[1,2]))\\%\$  	\\\\ \n")
write(io, "\$\\mathbb{E}[SP]\$ 					& Average bond spreads								& \$$(round(Int, target_mms[3,1]))\$bp 		& \$$(round(Int, target_mms[3,2]))\$bp 	\\\\ \n")
write(io, "\$\\sigma(SP)\$ 						& Volatility spreads								& \$$(round(Int, target_mms[4,1]))\$bp 		& \$$(round(Int, target_mms[4,2]))\$bp 	\\\\ \n")
write(io, "\$\\mathbb{P}[DF]\$ 					& Default frequency									& \$$(round(target_mms[2,1],1))\\%\$ 			& \$$(round(target_mms[2,2],1))\\%\$  		\\\\ \n")
write(io, "\$IIB_s / TD_s \$ 					& Inflation-indexed debt relative service			& \$$(round(Int, target_mms[5,1]))\\%\$ 		& \$$(round(Int, target_mms[5,2]))\\%\$  	\\\\ \n")
write(io, "\$\\eta_{{\\ensuremath{\\miniscule\\normalfont BE,SP}}} \$ 								& Semi-elasticity BE to spreads		& \$$(round(target_mms[6,1],2))\$ 	& \$$(round(target_mms[6,2],2))\$ 		\\\\ \n")
write(io, "\\hline \\hline \n")
write(io, "\\end{tabular} \n")
write(io, "\\end{table} \n")
end


### UNTARGETED MOMENTS - STANDARD MOMENTS
open("tables/table_untgt_stn.txt", "w") do io
write(io, "\\begin{table}[H] \n")
write(io, "\\centering \n")
write(io, "\\begin{tabular}{llcc} \n")
write(io, "\\hline \\hline \n")
write(io, " Target & Description & Data & Model \\\\ \n")
write(io, "\\hline \n")
write(io, "\$\\sigma(\\text{log}C) / \\sigma(\\text{log}Y)\$ 	& Relative volatility consumption					& \$$(round(untgt_mms[1,1], 2))\$ 			& \$$(round(untgt_mms[1,2], 2))\$  			\\\\ \n")
write(io, "\$\\sigma(TB/Y) / \\sigma(\\text{log}Y)\$ 			& Relative volatility trade balance					& \$$(round(untgt_mms[2,1], 2))\$ 			& \$$(round(untgt_mms[2,2], 2))\$  			\\\\ \n")
write(io, "\$\\text{corr}(\\text{log}C,\\text{log}Y)\$ 			& Correlation consumption \\& endowment				& \$$(round(Int, untgt_mms[3,1]))\\%\$ 		& \$$(round(Int, untgt_mms[3,2]))\\%\$  	\\\\ \n")
write(io, "\$\\text{corr}(TB/Y,\\text{log}Y)\$ 					& Correlation trade balance \\& endowment			& \$$(round(Int, untgt_mms[4,1]))\\%\$ 		& \$$(round(Int, untgt_mms[4,2]))\\%\$  	\\\\ \n")
write(io, "\$\\text{corr}(SP,\\text{log}Y)\$ 					& Correlation spreads \\& endowment					& \$$(round(Int, untgt_mms[5,1]))\\%\$ 		& \$$(round(Int, untgt_mms[5,2]))\\%\$  	\\\\ \n")
write(io, "\\hline \\hline \n")
write(io, "\\end{tabular} \n")
write(io, "\\end{table} \n")
end

### UNTARGETED MOMENTS - MODEL SPECIFIC MOMENTS
open("tables/table_untgt_piBE.txt", "w") do io
write(io, "\\begin{table}[H] \n")
write(io, "\\centering \n")
write(io, "\\begin{tabular}{llcc} \n")
write(io, "\\hline \\hline \n")
write(io, " Target & Description & Data & Model \\\\ \n")
write(io, "\\hline \n")
write(io, "\\textit{Panel A: Quarterly Frequency}  \\\\ \n")
write(io, "\\ \\ \$\\mathbb{E}[\\tilde{\\pi}]\$ 							  	  & Average inflation misreport									& \$$(round(piBE_mms[1,1],2))\\%\$ 			& \$$(round(piBE_mms[1,2],2))\\%\$  \\\\ \n")
write(io, "\\ \\ \$\\sigma(\\tilde{\\pi})\$ 									  & Volatility inflation misreport								& \$$(round(piBE_mms[2,1],2))\\%\$ 			& \$$(round(piBE_mms[2,2],2))\\%\$ 	\\\\ \n")
write(io, "\\ \\ \$\\text{corr}(\\tilde{\\pi},\\text{log}Y)\$ 					  & Correlation misreport \\& output								& \$$(round(Int, piBE_mms[6,1]))\\%\$ 	& \$$(round(Int, piBE_mms[6,2]))\\%\$  	\\\\ \n")
write(io, "\\textit{Panel B: High Frequency}  \\\\ \n")
write(io, "\\ \\ \$\\sigma(\\Delta BE)\$ 										  & Volatility break-even inflation								& \$$(round(piBE_mms[3,1],2))\\%\$ 			& \$$(round(piBE_mms[3,2],2))\\%\$ 	\\\\ \n")
write(io, "\\ \\ \$\\text{corr}(\\epsilon_{\\tilde{\\pi}},\\Delta BE)\$ 		  & Correlation misreport \\& break-even inflation	& \$$(round(Int, piBE_mms[5,1]))\\%\$ 	& \$$(round(Int, piBE_mms[5,2]))\\%\$  	\\\\ \n")
write(io, "\\ \\ \$\\text{corr}(\\epsilon_{\\tilde{\\pi}},\\Delta\\text{ln}SP)\$  & Correlation misreport \\& spread				    & \$$(round(Int, piBE_mms[4,1]))\\%\$ 	& \$$(round(Int, piBE_mms[4,2]))\\%\$  	\\\\ \n")
write(io, "\\hline \\hline \n")
write(io, "\\end{tabular} \n")
write(io, "\\end{table} \n")
end


### SPREAD DECOMPOSITION
open("tables/table_SpreadDecomp.txt", "w") do io
write(io, "\\begin{table}[H] \n")
write(io, "\\centering \n")
write(io, "\\begin{tabular}{llc} \n")
write(io, "\\hline \\hline \n")
write(io, " Moment & Description & Value \\\\ \n")
write(io, "\\hline \n")
write(io, "\$\\mathbb{E}[\\Upsilon]\$					& Average reputation premium 			       & \$$(round(Int, SPdcmp[1]))\$bp  \\\\ \n")
write(io, "\$\\mathbb{E}[\\Upsilon/SP]\$				& Incidence reputation premium on spreads      & \$$(round(Int, SPdcmp[2]))\\%\$ \\\\ \n")
write(io, "\$\\sigma(\\Upsilon)/\\sigma(SP)\$			& Reputation premium volatility     	   	   & \$$(round(Int, SPdcmp[6]))\\%\$ \\\\ \n")
write(io, "\$\\sigma(SP|\\zeta_H)/\\sigma(SP)\$			& Spread volatility under high reputation      & \$$(round(Int, SPdcmp[7]))\\%\$ \\\\ \n")
write(io, "\\hline \n")
write(io, "\$\\mathbb{E}[\\Upsilon/SP|Y<Y_l]\$			& Incidence with low output 			       & \$$(round(Int, SPdcmp[3]))\\%\$ \\\\ \n")
write(io, "\$\\text{corr}(\\Upsilon,\\text{log}Y)\$		& Correlation reputation premium \\& output    & \$$(round(Int, SPdcmp[4]))\\%\$ \\\\ \n")
write(io, "\$\\text{corr}(\\Upsilon/SP,\\text{log}Y)\$	& Correlation reputation incidence \\& output  & \$$(round(Int, SPdcmp[5]))\\%\$ \\\\ \n")
write(io, "\\hline \\hline \n")
write(io, "\\end{tabular} \n")
write(io, "\\end{table} \n")
end
